package com.sky.ok.okex.websocket.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.sky.ok.okex.common.OkFutureConst;
import com.sky.ok.okex.dao.DataDao;
import com.sky.ok.okex.entity.FuturesTicker;
import com.sky.ok.okex.entity.IndexTicker;
import com.sky.ok.okex.websocket.client.WebSocketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.ArrayList;

/**
 * 订阅信息处理类需要实现WebSocketService接口
 *
 * @author okcoin
 */
public class BuissnesWebSocketServiceImpl implements WebSocketService {

    private static final Logger log = LoggerFactory.getLogger("BuissnesWebSocketServiceImpl");

    @Autowired
    private DataDao dataWritedao;

    /*
     *
     * 把返回的数据存数据库
     * @author sky
     * @date 2018/10/12 10:10
     * @param [msg]
     * @return void
     */
    @Override
    public void onReceive(String msg) {
        //log.info("WebSocket Client received message: " + msg);

        JSONObject reMessage = JSONObject.parseObject(msg);


        if (reMessage.containsKey("data")) {
            String tableName = reMessage.get("table").toString();
            //日志太多，取消日志输出
           //log.info( reMessage.toJSONString());

            JSONArray tempJsonArray = (JSONArray) reMessage.get("data");
            //订阅现货行情
            if ("index/ticker".equals(tableName)) {
                log.info( reMessage.toJSONString());

                ArrayList<IndexTicker> data = JSON.parseObject(tempJsonArray.toJSONString(), new TypeReference<ArrayList<IndexTicker>>() {
                });
                for (IndexTicker dt : data) {
                    //System.out.println(dt.toString());
                }

            }
            //合约行情
            else if ("futures/ticker".equals(tableName)) {
                //log.info( reMessage.toJSONString());
                ArrayList<FuturesTicker> data = JSON.parseObject(tempJsonArray.toJSONString(), new TypeReference<ArrayList<FuturesTicker>>() {
                });
                for (FuturesTicker dt : data) {
                    System.out.println(dt.toString());
                }
            }
            //K线
            else if (OkFutureConst.SWAP_K_LIST.contains(tableName)) {
                //log.info( reMessage.toJSONString());
                //  System.out.println(tempJsonArray.toJSONString());
                JSONObject dataOne = (JSONObject) tempJsonArray.get(0);
               // System.out.println(dataOne.get("instrument_id"));
                //System.out.println(  (new JSONArray((JSONArray)dataOne.get("candle"))));

            }


        } else if (reMessage.containsKey("event")) {
            //订阅或者取消
            log.warn("WebSocket 状态发生改变！！！: " + msg);
        } else {
            log.error("返回的数据异常！！！！" + reMessage.toJSONString());
        }
    }
}
